//
//  WmcViewControllerEx.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/26.
//

import Foundation

extension UIViewController {
    
    /// 跳转到指定控制器
    /// - Parameter vcClass: 指定控制器class
    /// - Returns: description
    func popToSpecificVC(vcClass: AnyClass) -> Void {
        guard let vc = self.getSpecificVC(vcClass: vcClass) else {
            ///跨栈跳转 (此导航栈上找不到此控制器)
            if let tVC = self.navigationController?.viewControllers.first as? WmcTeamCloudAlbumHomeVC {
                tVC.tabBarController?.selectedIndex = 1
                self.navigationController?.popToViewController(tVC, animated: true)
            }
            return
        }
        self.navigationController?.popToViewController(vc, animated: true)
    }
    
    func popToSpecificVC(vc: WmcBaseVC) -> Void {
        self.navigationController?.popToViewController(vc, animated: true)
    }
    
    private func getSpecificVC(vcClass: AnyClass) -> UIViewController? {
        guard let controllers = self.navigationController?.viewControllers else {
            return nil
        }
        var temp: UIViewController?
        controllers.forEach { vc in
            if vc.isKind(of: vcClass) {
                temp = vc
            }
        }
        return temp
    }
    
}

extension WmcBaseVC {
    
    /// 找到返回标会到特定标记的控制器
    /// - Returns: description
    func findPopToMarkVC() -> WmcBaseVC? {
        guard let vcs = self.navigationController?.viewControllers as? [WmcBaseVC] else {
            return nil
        }
        var markedVC: WmcBaseVC?
        vcs.reversed().forEach { vc in
            if vc.popToVCMark {
                markedVC = vc
            }
        }
        return markedVC
    }
    
    func addNaviLineView() -> Void {
        
        navView.addSubview(naviLineView)
        naviLineView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
}
